package com.efounder.form.comp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.efounder.form.base.IComponent;
import com.efounder.form.group.IGroup;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.util.AppContext;
import com.efounder.util.StringUtil;

public class VGroup extends LinearLayout implements IComponent, IGroup, IUIComponent {
	public VGroup() {
		super(AppContext.getCurrentActivity());
		this.setOrientation(LinearLayout.VERTICAL);
	}

	public VGroup(Context context) {
		super(context);
	}


	public VGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private String id;

	private int height;
	private int width;

	private int gap = 0;

	private ViewSize size = new ViewSize(0,0);

	private int paddingLeft;
	private int paddingRight;
	private int paddingTop;
	private int paddingBottom;
	private int bkgColor;

	// 内部控件对齐方式
	private String horizontalAlign;
	private String verticalAlign;

	private float x;
	private float y;

	private int percentWidth;
	private int percentHeight;

	private Paint mPaint;
	private int borderWidth = 0;
	private int borderWeight = 0;
	private int borderColor = 0;

	private IUIComponent parentComp;


	@Override
	public void addChildComponent(IUIComponent component) {
		this.addView((View) component);
	}

	@Override
	public void creationComplete() {
	}

	@Override
	public void setLayout() {
		this.setBorder();
		FormCompUtil.setLayoutSize(this);
		this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign,verticalAlign));
		this.setLayoutPadding();
	}

	public void setBorder(){
		if (borderWidth <= 0 ) return;
		mPaint = new Paint();
		mPaint.setColor(StringUtil.formColorToColor(borderColor));
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(borderWidth);
		invalidate();
	}

	public int getBkgColor() {
		return bkgColor;
	}

	public void setBkgColor(int bkgColor) {
		this.bkgColor = bkgColor;

		this.setBackgroundColor(StringUtil.formColorToColor(bkgColor));

	}
	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);

		if(mPaint == null)	return;
		if (borderWidth <= 0 ) return;
		canvas.drawLine(0, 0, this.getWidth(), 0, mPaint);
		canvas.drawLine(0, this.getHeight(), this.getWidth(), this.getHeight(), mPaint);
		canvas.drawLine(0, 0, 0, this.getHeight(), mPaint);
		canvas.drawLine(this.getWidth(), 0, this.getWidth(), this.getHeight(), mPaint);
	}

	/*
     * LinearLayout边框
     */
	public void setLayoutPadding() {
		this.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
	}

	@Override
	public float getX() {
		return x;
	}

	@Override
	public void setX(float x) {
		this.x = x;
		this.setPivotX(x);
	}

	@Override
	public float getY() {
		return y;
	}

	@Override
	public void setY(float y) {
		this.y = y;
		this.setPivotY(y);
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}

	@Override
	public int getPercentWidth() {
		return percentWidth;
	}

	@Override
	public void setPercentWidth(int percentW) {
		percentWidth = percentW;
	}

	@Override
	public int getPercentHeight() {
		return percentHeight;
	}

	@Override
	public void setPercentHeight(int percentH) {
		percentHeight = percentH;
	}

	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	public String getHorizontalAlign() {
		return horizontalAlign;
	}

	public void setHorizontalAlign(String horizontalAlign) {
		this.horizontalAlign = horizontalAlign;
	}

	public String getVerticalAlign() {
		return verticalAlign;
	}

	public void setVerticalAlign(String verticalAlign) {
		this.verticalAlign = verticalAlign;
	}

	@Override
	public int getPaddingLeft() {
		return paddingLeft;
	}

	public void setPaddingLeft(int paddingLeft) {
		this.paddingLeft = paddingLeft;
	}

	@Override
	public int getPaddingRight() {
		return paddingRight;
	}

	public void setPaddingRight(int paddingRight) {
		this.paddingRight = paddingRight;
	}

	@Override
	public int getPaddingTop() {
		return paddingTop;
	}

	public void setPaddingTop(int paddingTop) {
		this.paddingTop = paddingTop;
	}

	@Override
	public int getPaddingBottom() {
		return paddingBottom;
	}

	public void setPaddingBottom(int paddingBottom) {
		this.paddingBottom = paddingBottom;
	}

	public int getGap() {
		return gap;
	}

	public void setGap(int gap) {
		this.gap = gap;
	}
	public int getBorderWidth() {
		return borderWidth;
	}

	public void setBorderWidth(int borderWidth) {
		this.borderWidth = borderWidth;
		//todo 因为换肤原因，这个属性去掉  yqs
		this.borderWidth = 0;

	}
	public int getBorderWeight() {
		return borderWeight;
	}

	public void setBorderWeight(int borderWeight) {
		this.borderWeight = borderWeight;
	}

	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}
}
